(function() {
    Nokia.Busy = Nokia.Widget.extend({
        initialize: function(options) {
            var instance = this;

			var defaults = {
				autoOpen: true,
				overflow: true,
				loaderText: false
            };

            instance.options = jQuery.extend(defaults, options);

            instance.element = jQuery( instance.options.element );

            instance._super.apply(instance, [instance.options]);

            instance.registerData("busy");

            instance._create();

            window.addEventListener('resize',function() {
				instance._positionLoader();
			}, false);

			instance.registerVibrationOn(['show']);
			instance.fireCallback('create');
        },

		destroy: function() {
			var instance = this;

			instance.overflow.remove();
			instance.loader.remove();

			instance.fireCallback('destroy');
		},

		hide: function() {
			var instance = this;

			instance.overflow.hide();
			instance.loader.hide();

			instance.fireCallback('hide');
		},

		show: function() {
			var instance = this;
			var element = instance.element;

			instance._showOverflow();
			instance._showLoader();
			instance._positionLoader();

			instance.fireCallback('show');
		},

		_create: function() {
			var instance = this;
			var options = instance.options;
			var element = instance.element;
			var Styles = instance.Styles;

			instance.isBody = (/(html|body)/).test(element[0].tagName.toLowerCase());

			instance.elementHeight = instance.isBody ? window.innerHeight : element.outerHeight();
			instance.elementWidth = instance.isBody ? window.innerWidth : element.outerWidth();

			element.each(function(i, el) {
				Nokia.util.addClass(el, Styles.busy);
			});

			instance._createOverflow();
			instance._createLoader();

			if ( options.autoOpen ) {
				instance.show();
			}
		},

		_createLoader: function(element) {
			var instance = this;
			var options = instance.options;
			var element = instance.element;
			var Styles = instance.Styles;
			var img = options.image;

			if (instance.loader) {
				instance.loader.remove();
			}

			if (options.loaderText) {
				instance.loader = Nokia.dom.parseHTML('<div class="' + Styles.busyTextLoader + '"><div>' + options.loaderText + '</div></div>');
			} else {
				instance.loader = Nokia.dom.parseHTML('<div class="' + Styles.busyLoader +'"></div>');
			}

			Nokia.dom.append(document.body, instance.loader);

			return instance.loader;
		},

		_createOverflow: function(element) {
			var instance = this;
			var element = instance.element;
			var Styles = instance.Styles;

			if (instance.overflow) {
				instance.overflow.remove();
			}

			instance.overflow = Nokia.util.createOverflow();

			return instance.overflow;
		},

		_showLoader: function() {
			var instance = this,
				loader = instance.loader;

			loader.show();
		},

		_positionLoader: function() {
			var instance = this,
				element = instance.element,
				loader = instance.loader;

			var offset = element.offset(),
				overflowHeight = instance.isBody ? window.innerHeight : instance.element.outerHeight(),
				overflowWidth = instance.isBody ? window.innerWidth : instance.element.outerWidth();

			var scrollTop = parseInt(jQuery(window).scrollTop(), 10);

			loader.css({
				top: offset.top + (overflowHeight - loader.height()) / 2 + scrollTop,
				left: offset.left + (overflowWidth - loader.width()) / 2
			});
		},

		_showOverflow: function() {
			var instance = this,
				element = instance.element,
				overflow = instance.overflow,
				offset = element.offset();

			var overflowHeight = instance.isBody ? window.innerHeight : instance.element.outerHeight();
			var overflowWidth = instance.isBody ? window.innerWidth : instance.element.outerWidth();

			overflow.css({
				top: instance.isBody ? jQuery(window).scrollTop() : offset.top,
				left: offset.left,
				height: overflowHeight,
				width: overflowWidth
			});

			Nokia.dom.append(document.body, instance.overflow);
			overflow[0].style.display = 'block';
		},

		Styles: {
			busy: 'nokia-busy',
			busyLoader: 'nokia-busy-loader',
			busyTextLoader: 'nokia-busy-text-loader',
			nokiaOverflow: 'nokia-overflow'
		}
    });
})();